package com.ruoyi.web.core.config;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.file.FileReader;
import cn.hutool.core.lang.Tuple;
import cn.hutool.core.map.MapUtil;
import com.ruoyi.dm.component.TleDataStrategy;

import java.io.BufferedReader;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.stream.Collectors;

public class TleUtil {


    /**
     *@Description 根据TLE文件路径，提取TLE数据, 先根据卫星编号分组，各组内再按照时间升序，最终返回 Map<卫星编号, 多个 TLE数据字符串>
     *@Author liuhl
     *@date 2024/4/30
     */
    public static Map<String,String> extractTleData(String filePath) throws Exception{
        //1 27391U 02012A   15001.94848096  .00014359  00000-0  21490-3 0  9993
        //2 27391 089.0043 089.5541 0012146 087.5798 009.3740 15.54771002716939
        BufferedReader reader = new FileReader(filePath).getReader();
        String line1 = "";
        String line2 = "";
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm-ss");
        Map<String, Map<String,String>> tleCodeAndDataMap = MapUtil.newTreeMap(Comparator.naturalOrder());
        while (null != (line1 = reader.readLine()) && null != (line2 = reader.readLine())) {
            String sateCode = line1.substring(2, 7).trim();
            Date date = TleDataStrategy.parseTleToDate(line1, pattern);
            String dateStr = DateUtil.format(date, DatePattern.PURE_DATETIME_FORMAT);
            String data = line1+"\n"+line2;

            Map<String, String> dateStrAndDataMap = tleCodeAndDataMap.get(sateCode);
            if (dateStrAndDataMap == null){
                dateStrAndDataMap = MapUtil.newTreeMap(Comparator.naturalOrder());
                dateStrAndDataMap.put(dateStr, data);
                tleCodeAndDataMap.put(sateCode, dateStrAndDataMap);
            }else {
                dateStrAndDataMap.put(dateStr, data);
            }
        }
        Map<String, String> resultMap = tleCodeAndDataMap.entrySet().stream().collect(Collectors.toMap(Map.Entry::getKey, entry -> {
            return entry.getValue().values().stream().collect(Collectors.joining("\n"));
        }));
        return resultMap;
    }


    /**
     *@Description 根据TLE文件路径，提取TLE数据, 先根据卫星编号分组，各组内再按照时间升序，最终返回 Map<卫星编号, 多个 TLE数据字符串>
     *@Author liuhl
     *@date 2024/4/30
     */
    public static Map<String, Tuple> extractSpecialTleData(String filePath) throws Exception{
        //00900 //卫星编号
        //30    //单位是km，前端在表头展示 km
        //1 27391U 02012A   15001.94848096  .00014359  00000-0  21490-3 0  9993
        //2 27391 089.0043 089.5541 0012146 087.5798 009.3740 15.54771002716939
        BufferedReader reader = new FileReader(filePath).getReader();
        String sateCode = "";
        String height = "";
        String line1 = "";
        String line2 = "";
        Map<String, Tuple> tleCodeAndDataMap = MapUtil.newTreeMap(Comparator.naturalOrder());
        while ( null != (sateCode = reader.readLine())
                && null != (height = reader.readLine())
                && null != (line1 = reader.readLine())
                && null != (line2 = reader.readLine())) {

            String data = line1+"\n"+line2;
            tleCodeAndDataMap.putIfAbsent(sateCode,new Tuple(height,data));
        }
        return tleCodeAndDataMap;
    }


}
